package com.css.fxfzypg.modules.resultsManagement.service.impl;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.resultsManagement.entity.MapTemplate;
import com.css.fxfzypg.modules.resultsManagement.entity.MapType;
import com.css.fxfzypg.modules.resultsManagement.repository.MapTemplateRepository;
import com.css.fxfzypg.modules.resultsManagement.repository.MapTypeRepository;
import com.css.fxfzypg.modules.resultsManagement.service.MapTypeService;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.PlatformSessionUtils;
import com.css.fxfzypg.util.UUIDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
public class MapTypeServiceImpl implements MapTypeService {
    @Resource
    MapTypeRepository mapTypeRepository;
    @Resource
    MapTemplateRepository mapTemplateRepository;

    @Override
    public RestResponse saveMapType(MapType mapType) {
        mapType.setId(UUIDGenerator.getUUID());
        mapType.setDelFlag(YNEnum.N.toString());
        mapType.setCreateUser(PlatformSessionUtils.getUserId());
        mapType.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        mapTypeRepository.saveMapType(mapType);
        return RestResponse.succeed("保存成功");
    }

    @Override
    public MapType getById(String id) {
        return mapTypeRepository.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String id) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        mapTypeRepository.deleteById(id, timestamp);
        mapTemplateRepository.delete(id,timestamp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MapType update(MapType mapType) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        String userId = PlatformSessionUtils.getUserId();
        mapType.setUpdateTime(timestamp);
        mapType.setUpdateUser(userId);
        mapTemplateRepository.updateFlag(mapType.getId(),mapType.getEnableFlag(),timestamp);
        return mapTypeRepository.update(mapType);
    }

    @Override
    public Map<String, Object> queryMapTypes(MapType mapType, int curPage, int pageSize) {
        Map<String, Object> map = mapTypeRepository.queryMaps(mapType, curPage, pageSize);
        return map;
    }

    @Override
    public RestResponse checkType(MapType mapType) {
        MapType mapType1 = mapTypeRepository.getByMapType(mapType.getMapType(),mapType.getId(),mapType.getReportType());
        if (mapType1 == null) {
            return RestResponse.succeed(false);
        }
        return RestResponse.succeed(true);
    }

    @Override
    public List<MapType> getByReportType(String reportType) {
        return mapTypeRepository.getByReportType(reportType);
    }

    @Override
    public RestResponse check(String id) {
        //根据类型id查是否有模板信息
        List<MapTemplate> mapTemplates=mapTemplateRepository.getByTypeId(id);
        if (mapTemplates.size()==0){
            return RestResponse.succeed(false);
        }
        return RestResponse.succeed(true);
    }
}
